﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;

namespace MyMegafon
{
    public partial class Form1 : Form
    {
        private MessageInterceptor _smsInterceptor;
        private MessageInterceptor _smsInterceptor2;
        readonly Balans _balance = new Balans();
        const string strProvBals = "Выполняется проверка баланса";
        const string strProvSkid = "Выполняется проверка скидок";

        public Form1()
        {
            Cursor.Current = Cursors.WaitCursor;
            InitializeComponent();
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Закрыть программу?", "Диалог закрытия", MessageBoxButtons.OKCancel,
                                    MessageBoxIcon.Question
                                    , MessageBoxDefaultButton.Button1) == DialogResult.OK)
            Application.Exit();
        }

        private void comboBoxDelta_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Меняем и высчитываем дельту на новую
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBoxDelta.SelectedIndex = 0;
            _smsInterceptor = new MessageInterceptor(InterceptionAction.NotifyAndDelete, true)
                                  {
                                      MessageCondition = new MessageCondition(
                                          MessageProperty.Sender,
                                          MessagePropertyComparisonType.Equal,
                                          "000100",
                                          true)
                                  };
            _smsInterceptor.MessageReceived += smsInterceptor_MessageReceived;
            _smsInterceptor2 = new MessageInterceptor(InterceptionAction.NotifyAndDelete, true)
                                   {
                                       MessageCondition = new MessageCondition(
                                           MessageProperty.Sender,
                                           MessagePropertyComparisonType.Equal,
                                           "000102",
                                           true)
                                   };
            _smsInterceptor2.MessageReceived += smsInterceptor2_MessageReceived;

            labelBalansNow2.Text = _balance.GetBalansFromFile();

            try
            {
                _balance.Balance = double.Parse(labelBalansNow2.Text);
            }
            catch
            {
            }
        }

        private void smsInterceptor_MessageReceived(object sender, MessageInterceptorEventArgs e)
        {
            timer1.Enabled = false;
            // Обработка входящего смс
            var sms = (SmsMessage) e.Message;
            var ssms = sms.Body;
            if (ssms.StartsWith("Ошибка"))
            {
                labelBalansNow2.Text = ssms;
                return;
            }
            string[] balans = ssms.Split(new Char[] {});
            labelBalansNow2.Text = balans[1];
            labelBalansNow2.TextAlign = ContentAlignment.TopCenter;
            _balance.SetNowBalace(balans[1]);
            labelDeltaBalans2.Text = _balance.GetBalanseDeltaOld().ToString();
            _balance.SaveBalansToFile();
        }

        private void smsInterceptor2_MessageReceived(object sender, MessageInterceptorEventArgs e)
        {
            timer2.Enabled = false;
            // Обработка входящего смс
            var sms = (SmsMessage)e.Message;
            labelOffers2.Text = sms.Body;
            labelOffers2.TextAlign = ContentAlignment.TopCenter;
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            labelBalansNow2.Text = strProvBals;
            labelBalansNow2.TextAlign = ContentAlignment.TopLeft;
            labelOffers2.Text = strProvSkid;
            labelOffers2.TextAlign = ContentAlignment.TopLeft;
            timer1.Enabled = true;
            timer2.Enabled = true;
            _balance.Refresh();
        }

        private void menuItemAbout_Click(object sender, EventArgs e)
        {
            var assem = Assembly.GetExecutingAssembly().GetName();
            MessageBox.Show(assem.Name +" "+assem.Version.Major+"."+assem.Version.Minor
                +"\n\nCopyright (c)2009 W&B Soft Mobile", "О программе"
            , MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
        }

        private void Form1_Closing(object sender, CancelEventArgs e)
        {
            if (e.Cancel == false)
            {
                if (MessageBox.Show("Закрыть программу?", "Диалог закрытия", MessageBoxButtons.OKCancel,
                                    MessageBoxIcon.Question
                                    , MessageBoxDefaultButton.Button1) == DialogResult.Cancel)
                    e.Cancel = true;
            }
        }

        [DllImport("coredll.dll")]
        static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
        const int SW_MINIMIZED = 6;

        private void menuItemMinimize_Click(object sender, EventArgs e)
        {
            ShowWindow(this.Handle, SW_MINIMIZED);
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            var f2 = new FormData();
            f2.ShowDialog();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (labelBalansNow2.Text.EndsWith("..."))
            {
                labelBalansNow2.Text = labelBalansNow2.Text.Substring(0, labelBalansNow2.Text.Length - 3); ;
            }
            else
            {
                labelBalansNow2.Text += ".";
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if ( labelOffers2.Text.EndsWith("..."))
            {
                labelOffers2.Text = labelOffers2.Text.Substring(0, labelOffers2.Text.Length - 3);
            }
            else
            {
                labelOffers2.Text += ".";
            }
        }
    }
}